SYS_NAME=test

MAINPATH=$(shell /bin/pwd)
CPROJECTPATH=$(MAINPATH)/CProject
CPPPROJECTPATH=$(MAINPATH)/CppProject
COMMONPATH=$(MAINPATH)/Common
TESTPATH=$(MAINPATH)/TestProject
BINARY=binary

INCLUDES=-I$(CPROJECTPATH)/headers -I$(COMMONPATH)/headers -I$(CPPPROJECTPATH)/headers
vpath %.c CProject/sources
vpath %.cpp CppProject/sources
vpath %.c $(COMMONPATH)/sources
vpath %.c TestProject/Test_files
vpath %.cpp TestProject/Test_files

C_OBJECTS=$(BINARY)/Dual_Link_List.o \
          $(BINARY)/thread_routine.o \
          $(BINARY)/alarm_thread.o \
          $(BINARY)/alarm_data_mutex.o \
   	      $(BINARY)/datapool_mutex.o \
          $(BINARY)/data_read_thread.o \
          $(BINARY)/data_process_thread.o \
   	      $(BINARY)/data_write_thread.o \
   	      $(BINARY)/insert_alarm.o	\
          $(BINARY)/tcp_server.o	\
          $(BINARY)/tcp_client.o	\
          $(BINARY)/udp_server.o	\
          $(BINARY)/udp_client.o	\
          $(BINARY)/parallel_tcp_server.o
		       
CPP_OBJECTS=$(BINARY)/StlVerification.o

C_UNITTEST_OBJECTS=$(BINARY)/CreateLinkListHelper.o \
	               $(BINARY)/PrintDataHelper.o \
	               $(BINARY)/test.o
	             
CPP_UNITTEST_OBJECTS=$(BINARY)/cpp_test.o
	             
ifdef C_PROJECT
SYSTEM_OBJECTS = $(C_OBJECTS)
UNITTEST_OBJECTS = $(C_UNITTEST_OBJECTS)
endif

ifdef CPP_PROJECT
SYSTEM_OBJECTS = $(CPP_OBJECTS)
UNITTEST_OBJECTS = $(CPP_UNITTEST_OBJECTS)
endif

SUN_STUDIO = /usr
SUN_STUDIO_PREFIX = $(SUN_STUDIO)
CFLAGS=$(INCLUDES) -g 
CC = $(SUN_STUDIO_PREFIX)/bin/gcc
CXX = $(SUN_STUDIO_PREFIX)/bin/g++
MKDIR=mkdir

#------------------------------------------------------make c binaries-----------------------------------------------------------#

ifdef C_PROJECT

all : $(BINARY) $(SYS_NAME) assist_test

$(BINARY):
	$(MKDIR) $(BINARY)
	
$(SYS_NAME): $(SYSTEM_OBJECTS) $(UNITTEST_OBJECTS)
	$(CC) -o $(SYS_NAME) $(SYSTEM_OBJECTS) $(UNITTEST_OBJECTS) -lpthread

$(BINARY)/%.o : %.c 
	$(CC) $(CFLAGS) -o $@ -c $<
	
assist_test: $(BINARY)/assist_test.o $(BINARY)/tcp_server.o $(BINARY)/udp_server.o $(BINARY)/parallel_tcp_server.o
	gcc -o assist_test $(BINARY)/assist_test.o $(BINARY)/tcp_server.o $(BINARY)/udp_server.o $(BINARY)/parallel_tcp_server.o

endif

#------------------------------------------------------make c++ binaries--------------------------------------------------------#

ifdef CPP_PROJECT

all : $(BINARY) $(SYS_NAME)

$(BINARY):
	$(MKDIR) $(BINARY)

$(SYS_NAME): $(SYSTEM_OBJECTS) $(UNITTEST_OBJECTS)
	$(CXX) -o $(SYS_NAME) $(SYSTEM_OBJECTS) $(UNITTEST_OBJECTS) -lpthread

$(BINARY)/%.o : %.cpp 
	$(CXX) $(CFLAGS) -o $@ -c $<
endif

#------------------------------------------------------clean all binaries--------------------------------------------------------#

clean:
	rm -rf $(SYS_NAME) $(BINARY)
	rm -rf assist_test
